var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    viewName: '',
    name: '',
    userName: '',
    type: 1
  },
  onLoad: function(options) {},
  onReady: function() {},
  onShow: function() {
    console.log(this.options)
    var _this = this,
      id = this.options.id
    if (id == 2) {
      _this.setData({
        viewName: this.options.cover_url,
        name: this.options.cover,
        userName: this.options.name,
        type: id
      })
    }
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function() {},
  onPullUser(e) {
    var _this = this,
      v = e.detail.value
    _this.setData({
      userName: v
    })
  },
  onUpdateImage(e) {
    var id = e.currentTarget.id,
      _this = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        wx.uploadFile({
          url: app.link.temporary,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            file: res.tempFilePaths[0]
          },
          success: function(res) {
            console.log(res)
            let img = JSON.parse(res.data)
            console.log(img)
            _this.setData({
              viewName: img.response.file.http,
              name: img.response.file.name
            })
          }
        })
      }
    })
  },
  onUserlist(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      if (!_this.data.userName) {
        wx.showToast({
          title: '请填写验光师姓名',
          icon: 'none'
        })
        return false;
      }
      if (!_this.data.name) {
        wx.showToast({
          title: '请上传验光师照片',
          icon: 'none'
        })
        return false;
      }
      com.link(app.link.optiometUser, {
        name: _this.data.userName,
        cover: _this.data.name
      }, function(res) {
        if (res.data.state) {
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.message,
          })
        }
        console.log(res)
      })
    } else {
      com.link(app.link.onUserUpdate + this.options.uid + '.st', {
        name: _this.data.userName,
        cover: _this.data.name
      }, function(rep) {
        if (rep.data.state) {
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.message,
          })
        }
      })
    }

  },
})